let customerListModule = (function () {
    //lx=my&type=''&search=''&limit=10&page=1
//    lx=my获取我的客户
//    lx=all获取全部客户（默认）
    let $selectBox = $('.selectBox'),
        $searchInp = $('.searchInp'),
        $tableBox = $('.tableBox'),
        $tbody = $tableBox.find('tbody'),
        $pageBox=$('.pageBox')
    
    let lx = 'my',
        limit = 10,
        page = 1,
        total = 0,
        totalPage = 1
    
    // 列表绑定数据
    async function bindHTML() {
        let result = await axios.get('/customer/list', {
            params: {
                lx,
                type: $selectBox.val(),
                search: $searchInp.val().trim(),
                limit,
                page
            }
        })
        $tbody.html('')
        $pageBox.html('')
        if (result.code != 0) return
        
        total = parseInt(result.total)
        totalPage = parseInt(result.totalPage) 
        
        // 绑定表格
        let data = result.data
        let str = ``
        data.forEach(item => {
            let { id,
                name,
                sex,
                email,
                phone,
                QQ,
                weixin,
                type,
                address,
                userId,
                userName
            } = item
            
            str += `<tr>
                    <td class="w8">${name}</td>
                    <td class="w5">${sex == 0 ? '男' : '女'}</td>
                    <td class="w10">${email}</td>
                    <td class="w10">${phone}</td>
                    <td class="w10">${weixin}</td>
                    <td class="w10">${QQ}</td>
                    <td class="w5">${type}</td>
                    <td class="w8">${userName}</td>
                    <td class="w20">${address}</td>
                    <td class="w14" customerid=${id}>
                        <a href="customeradd.html?userid=${id}" class="btnEdit">编辑</a>
                        <a href="javascript:;" class="btnDelete">删除</a>
                        <a href="visit.html?id=${id}" >回访记录</a>
                    </td>
                </tr>`
        })
        $tbody.html(str)
 
        // 分页绑定
        if (totalPage > 1) {
            str = ``
            page > 1 ? str += `<a href="javascript:;" class="prePage">上一页</a>` : null
            str += `<ul class="pageNum">`
            for (let i = 1; i <= totalPage; i++) {
                str += `<li class="${i == page ? 'active' : ''}">${i}</li>`
            }
            str += `</ul>`
            page < totalPage ? str += `<a href="javascript:;" class="nextPage">下一页</a>` : null
            $pageBox.html(str) 
        }   
    }

    // 操作处理
    function handle() {
        // 类型
        $selectBox.change(bindHTML)
        // 搜索
        $searchInp.keydown(ev => {
            if (ev.keyCode === 13) {
                bindHTML()
            }
        })
        // 分页
        $pageBox.click(ev => {
            let target = ev.target,
                TAG = target.tagName,
                TEXT = target.innerHTML,
                temp = page 
            if (target.className === 'prePage') {
                temp-- 
            }
            if (target.className === 'nextPage') {
                temp++ 
            }
            if (TAG === "LI") {
                temp = parseInt(TEXT)
            }
            temp !== page? (page=temp,bindHTML()):null  
        })
        // 数据操作
        $tbody.click(async ev => {
            let target = ev.target,
                $target = $(target)
            let customerid= $target.parent().attr('customerid') 
            if (target.className === 'btnDelete') {
                let flag=confirm(`确定要删除编号为[${customerid}]的信息吗？`)
                if (!flag) return
                let result = await axios.get('/customer/delete', { params: { customerId: customerid } })
                if (result.code == 0) {
                    alert('删除成功')
                    page = 1 
                    bindHTML()
                    return
                }
                alert('删除失败')
                return
            }
            

        })
    }
    return {
        init() {
            let params = window.location.href.queryURLParams()
            params.lx ? lx = params.lx : null
            
            bindHTML()
            handle()
        }
    }
})()
customerListModule.init()